package com.dream.dao.stock;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import java.util.Date;

import org.springframework.stereotype.Repository;

import com.dream.model.stock.ProductPartRequest;
import com.dream.model.stock.ProductPartRequest.RequestStatus;
import com.dream.model.stock.ProductPartRequest.StockDirection;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.stock.ProductPartStockVO;

@Repository
public class ProductPartRequestDAO extends DreamBaseDAO<ProductPartRequest> {
	
	public PagingVO query(final ProductPartStockVO vo, final PagingVO page){
		
		DetachedCriteriaBuilder queryBuilder = initQueryBuilder(vo);
		DetachedCriteriaBuilder countBuilder = initQueryBuilder(vo);
		return this.selectPagingVO(queryBuilder, page, countBuilder);
		
	}
	
	private DetachedCriteriaBuilder initQueryBuilder(final ProductPartStockVO vo){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(ProductPartRequest.class);
		builder.innerJoin("productPart", "part");
		builder.addEq("deleted", false).addEq("manualId", vo.getManualId());
		builder.addEq("requestType", StockDirection.getInstance(vo.getRequestType()));
		builder.addEq("part.id", vo.getProductPartId()).addLikeAny("part.name", vo.getProductPartName());
		builder.addEq("requestUser", vo.getRequestUser());
		builder.addGe("requestDate", vo.getStart()).addLe("requestDate", vo.getEnd());
		builder.addIn("status", vo.getStatus());
		//总部审核
		if(vo.getStatus().contains(RequestStatus.submit)){
			builder.addIn("fromShopId", vo.getMyShops());
		}
		
		//发货
		if(vo.getStatus().contains(RequestStatus.approve)){
			
			if(StockDirection.in.equals(StockDirection.getInstance(vo.getRequestType()))){
				//总部发货
				builder.addIn("fromShopId", vo.getMyShops());
			}
			
			if(StockDirection.out.equals(StockDirection.getInstance(vo.getRequestType()))){
				//网点发货
				builder.addIn("toShopId", vo.getMyShops());
			}
		} 
		
		//确认收货
		if(vo.getStatus().contains(RequestStatus.post)){
			
			if(StockDirection.in.equals(StockDirection.getInstance(vo.getRequestType()))){
				//网点确认收货
				builder.addIn("toShopId", vo.getMyShops());
			}
			
			if(StockDirection.out.equals(StockDirection.getInstance(vo.getRequestType()))){
				//总部确认收货
				builder.addIn("fromShopId", vo.getMyShops());
			}
		}
		
		return builder;
		
	}
	
	public PagingVO queryHistory(final ProductPartStockVO vo, final PagingVO page){
		DetachedCriteriaBuilder queryBuilder = initHisotryQueryBuilder(vo);
		DetachedCriteriaBuilder countBuilder = initHisotryQueryBuilder(vo);
		return this.selectPagingVO(queryBuilder, page, countBuilder);
	}
	
   private DetachedCriteriaBuilder initHisotryQueryBuilder(final ProductPartStockVO vo){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(ProductPartRequest.class);
		builder.innerJoin("productPart", "part").innerJoin("toShop", "ts");
		builder.addEq("deleted", false).addEq("manualId", vo.getManualId());
		builder.addEq("requestType", StockDirection.getInstance(vo.getRequestType()));
		builder.addEq("part.id", vo.getProductPartId()).addLikeAny("part.name", vo.getProductPartName());
		builder.addEq("requestUser", vo.getRequestUser());
		builder.addGe("requestDate", vo.getStart()).addLe("requestDate", vo.getEnd());
		builder.addIn("status", vo.getStatus());
		builder.addIn("ts.orgId", vo.getOrgs());
		
		return builder;
		
	}
	
	public void doActionRequest(RequestStatus status,Long requestId,String user){
		
		final String hql = "update ProductPartRequest set status=?,updateTime=?,updateUser=? where id =?";
		this.bulkUpdate(hql, status,new Date(),user,requestId);
	}
	
}
